package mysql;

import xdb.Trace;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class MySQLConf {
    private String ip;
    private int port;
    private String dbName;
    private int poolSize;
    private String user;
    private String password;
    private int msDebug;
    private int msInfo;
    private int minPoolSize;
    private int maxPoolSize;
    private boolean bAutoSql;
    private boolean bCloseCache;
    private String version;


    public boolean Init() {
        return this.Init("db.conf");
    }

    public boolean Init(String fileName) {
        Properties prop = new Properties();

        try {
            FileInputStream finput = new FileInputStream(fileName);
            prop.load(finput);
            finput.close();
        } catch (FileNotFoundException e) {
            Trace.error("Can't Find File db.conf", e);
            return false;
        } catch (IOException e) {
            Trace.error("Read File Error db.conf", e);
            return false;
        }

        if (prop.getProperty("IP") == null) {
            Trace.error("Can't Find Property [IP]");
            return false;
        } else {
            return Init(prop);
        }
    }

    public boolean Init(Properties prop) {
        if (prop.getProperty("IP") == null) {
            Trace.error("Can't Find Property [IP]");
            return false;
        }
        this.setIp(prop.getProperty("IP"));
        if (prop.getProperty("PORT") == null) {
            Trace.error("Can't Find Property [PORT]");
            return false;
        }
        this.setPort(Integer.valueOf(prop.getProperty("PORT")));

        if (prop.getProperty("NAME") == null) {
            Trace.error("Can't Find Property [NAME]");
            return false;
        }
        this.setDbName(prop.getProperty("NAME"));

        if (prop.getProperty("USER") == null) {
            Trace.error("Can't Find Property [USER]");
            return false;
        }
        this.setUser(prop.getProperty("USER"));

        if (prop.getProperty("PASSWORD") == null) {
            Trace.error("Can't Find Property [PASSWORD]");
            return false;
        }
        this.setPassword(prop.getProperty("PASSWORD"));

        if (prop.getProperty("POOLSIZE") == null) {
            Trace.error("Can't Find Property [POOLSIZE]");
            return false;
        }
        this.setVersion(prop.getProperty("VERSION", "5.7"));
        this.setMsDebug(Integer.parseInt(prop.getProperty("MS_DEBUG", "5")));
        this.setMsInfo(Integer.parseInt(prop.getProperty("MS_INFO", "50")));
        this.setPoolSize(Integer.valueOf(prop.getProperty("POOLSIZE")));
        this.setMinPoolSize(Integer.valueOf(prop.getProperty("MIN_POOL_SIZE", this.poolSize / 3 + 1 + "")));
        this.setMaxPoolSize(Integer.valueOf(prop.getProperty("MAX_POOL_SIZE", this.poolSize + "")));
        this.setbAutoSql(Boolean.valueOf(prop.getProperty("AUTOSQL", "false")));
        this.setbCloseCache(Boolean.valueOf(prop.getProperty("CLOSECACHE", "false")));
        Trace.info(toString());
        return true;
    }

    public String getIp() {
        return this.ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return this.port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getDbName() {
        return this.dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public int getPoolSize() {
        return this.poolSize;
    }

    public void setPoolSize(int poolSize) {
        this.poolSize = poolSize;
    }

    public String getUser() {
        return this.user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMsDebug() {
        return this.msDebug;
    }

    public void setMsDebug(int msDebug) {
        this.msDebug = msDebug;
    }

    public int getMsInfo() {
        return this.msInfo;
    }

    public void setMsInfo(int msInfo) {
        this.msInfo = msInfo;
    }

    public int getMinPoolSize() {
        return this.minPoolSize;
    }

    public void setMinPoolSize(int minPoolSize) {
        this.minPoolSize = minPoolSize;
    }

    public int getMaxPoolSize() {
        return this.maxPoolSize;
    }

    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public boolean isbAutoSql() {
        return this.bAutoSql;
    }

    public void setbAutoSql(boolean bAutoSql) {
        this.bAutoSql = bAutoSql;
    }

    public boolean isbCloseCache() {
        return this.bCloseCache;
    }

    public void setbCloseCache(boolean bCloseCache) {
        this.bCloseCache = bCloseCache;
    }

    public String getVersion() {
        return this.version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append(this.ip + ",");
        sb.append(this.port + ",");
        sb.append(this.dbName + ",");
        sb.append(this.version + ",");
        sb.append(this.poolSize + ",");
        sb.append(this.minPoolSize + ",");
        sb.append(this.maxPoolSize + ",");
        sb.append(this.bAutoSql + ",");
        sb.append(this.bCloseCache + ",");
        sb.append(this.msDebug).append(",").append(this.msInfo).append(",");
        sb.append(this.user + "]");
        return sb.toString();
    }
}
